package com.bodu.member.controller;

import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.entity.result.Result;
import com.bodu.member.client.UserClient;
import com.bodu.member.entity.request.MemberUserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "登录页面API", tags = "登录页面API")
@RestController
@RequestMapping("api/user")
public class UserApiController {

    private final UserClient userClient;

    public UserApiController(UserClient userClient) {
        this.userClient = userClient;
    }

    @ApiOperation("获取微信openid信息")
    @GetMapping("getWeixinOpenid")
    public Result getWeixinOpenid(String js_code) {
        try {
            return userClient.getWeixinOpenid(js_code);
        } catch (Exception e) {
            return Result.fail("得到openid失败");
        }
    }

    @GetMapping("getUser")
    @ApiOperation("得到用户信息")
    public Result getUser(String openid) {
        return userClient.getUser(openid);
    }

    @PostMapping("saveUser")
    @ApiModelProperty("保存用户信息")
    public Result saveUser(@RequestBody MemberUserRequest userRequest) {
        userRequest.setStatusFlag(StatusFlagEnum.USE.getValue());
        userRequest.setDelFlag(DelFlagEnum.NO.getValue());
        return userClient.saveUser(userRequest);
    }

}
